//
//  NewfeatureCollectionViewController.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/4.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

private let reuseIdentifier = "frank"

class NewfeatureCollectionViewController: UICollectionViewController {
    
    // 页面个数
    private let pageCount = 4
    // 布局对象
    private var layout : UICollectionViewFlowLayout = NewfeatureLayout()
   
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  pageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!  NewfeatureCell
      cell.imageIndex = indexPath.row
    
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //  显示当前可见的cell 的数组
        
         let index =  collectionView.indexPathsForVisibleItems().last!
         if index.item == ( pageCount - 1){
            
            let newCell = cell as! NewfeatureCell
            newCell.startButtonAnimation()
        }
    }
}

private class NewfeatureCell : UICollectionViewCell {
    
    // swift被private修饰的东西， 如果实在同一文件中是可以访问的
    private var imageIndex: Int? {
        didSet {
            self.img.image = UIImage(named:"new_feature_\(imageIndex! + 1)")
            self.startButton.hidden = imageIndex == 3 ? false : true
        }
    }
    
    
    // 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(img)
        contentView.addSubview(startButton)
        
        startButton.snp_makeConstraints { (make) in
            make.width.equalTo(136)
            make.height.equalTo(42)
            make.bottom.equalTo(contentView).offset(-120)
            make.centerX.equalTo(contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
      开始动画
     */
    func startButtonAnimation() {
        let anim: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
        anim.duration = 0.4
        anim.fromValue = 0.0
        anim.toValue = 2.0
       // anim.repeatCount = 1
        
        //anim.fillMode = kCAFillModeForwards
        startButton.layer.addAnimation(anim, forKey: nil)
    }
    
    @objc func xxxClick(){
        // 授权成功后，去tabbar
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchWindowRootViewController, object: true)
    }

    
    /// 懒加载img
    private lazy var img : UIImageView = {
        let img = UIImageView()
        img.frame = self.bounds
        return img
    }()

    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        
      //  btn.hidden = true
        btn.addTarget(self, action: #selector(NewfeatureCell.xxxClick), forControlEvents: UIControlEvents.TouchUpInside)
      
        return btn
    }()
    
}



private class NewfeatureLayout : UICollectionViewFlowLayout{
    
    private override func prepareLayout() {
        
     itemSize = UIScreen.mainScreen().bounds.size
     minimumInteritemSpacing = 0
     minimumLineSpacing = 0
     scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        
    collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
    }
}
